//
//  ShellWallertHeadView.m
//  ShellWallert
//
//  Created by lvzhao on 2019/9/27.
//  Copyright © 2019年 VV. All rights reserved.
//

#import "ShellWallertHeadView.h"
#import "ShellWallertViewModel.h"
#import "ShellRechargeViewController.h" //提币
#import "ShellLiquidationViewController.h"      //清算
#import "ShellPledgeViewController.h"       //质押
#import "ShellWithdrawViewController.h"         //充值
#import "ShellProfitViewController.h"          //收益
#import "ShellExchangeViewController.h"

@interface  ShellWallertHeadView()

@property (nonatomic,strong) ShellWallertViewModel *viewModel;

//背景View
@property (nonatomic,strong) UIView *contentView;
//货币图片
@property (nonatomic,strong) UIImageView *currencyImageView;

//清算
@property (nonatomic,strong) UIButton *liquidationBtn;

//数量
@property (nonatomic,strong) UILabel *numberLab;

//冻结数量
@property (nonatomic,strong) UILabel *frozenNumLabel;

//可用数量
@property (nonatomic,strong) UILabel *availableNumLabel;

//质押
@property (nonatomic,strong) UIButton *pledgeBtn;


@end

@implementation ShellWallertHeadView

- (instancetype)initViewModel:(LZBaseViewModel *)viewModel
{
    self = [super init];
    if (self) {
        
        self.viewModel = (ShellWallertViewModel *)viewModel;
        [self setupView];
    }
    return self;
}

#pragma mark - setupView
- (void)setupView{
    
    
    [self addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(15);
        make.width.mas_equalTo(K_SCREENWIDTH - 30);
        make.height.mas_equalTo(290);
    }];
    
    UIView *bgimgView = [[UIView alloc] init];
    bgimgView.backgroundColor = COLOR_BACKGROUND;
    [self addSubview:bgimgView];
    bgimgView.cornerRadius = 30;
    [bgimgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView.mas_top).mas_offset(-5);
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(60);
    }];
    
    [bgimgView addSubview:self.currencyImageView];
    [self.currencyImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(bgimgView.mas_centerY);
        make.centerX.mas_equalTo(bgimgView.mas_centerX);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(50);
    }];
    
    ///!!!:持有数量
    UILabel *holdLabel = [UILabel lz_labelWithText:LZLocalizedString(@"持有数量") fontSize:k_textFont(13) color:UIColorHex(0xFFFFFF)];
    [self.contentView addSubview:holdLabel];
    [holdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(60);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(17);
    }];
    
    [self.contentView addSubview:self.numberLab];
    [self.numberLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(holdLabel.mas_bottom);
        make.width.mas_equalTo(180);
        make.height.mas_equalTo(42);
    }];
    
    
    ///!!!:提币
    UIButton *coinBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"提币") titleColor:UIColorHex(0x576E98) fontSize:12];
    CGFloat coinBtnW = [coinBtn.currentTitle lz_textWidthWithFontSize:coinBtn.titleLabel.font withMaxHeight:17];
    [self.contentView addSubview:coinBtn];
    [coinBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-28);
        make.top.mas_equalTo(90);
        make.width.mas_equalTo(coinBtnW);
        make.height.mas_equalTo(20);
    }];
    
    ///!!!:清算
    [self.contentView addSubview:self.liquidationBtn];
    CGFloat liquidationBtnW = [self.liquidationBtn.currentTitle lz_textWidthWithFontSize:self.liquidationBtn.titleLabel.font withMaxHeight:17];
    self.liquidationBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [self.liquidationBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(coinBtn.mas_centerY);
        make.right.mas_equalTo(coinBtn.mas_left).mas_offset(-20);
        make.width.mas_equalTo(liquidationBtnW);
        make.height.mas_equalTo(20);
    }];
    

    
    ///!!!:冻结数量
    UILabel *frozenLabel = [UILabel lz_labelWithText:LZLocalizedString(@"冻结数量") fontSize:k_textFont(13) color:UIColorHex(0xFFFFFF)];
    [self.contentView addSubview:frozenLabel];
    [frozenLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(140);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(17);
    }];
    
    [self.contentView addSubview:self.frozenNumLabel];
    [self.frozenNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(frozenLabel.mas_top);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(17);
    }];
    
    
    
    ///!!!:可用数量
    UILabel *availableLabel = [UILabel lz_labelWithText:LZLocalizedString(@"可用数量") fontSize:k_textFont(13) color:UIColorHex(0xFFFFFF)];
    [self.contentView addSubview:availableLabel];
    [availableLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(frozenLabel.mas_bottom).mas_offset(10);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(17);
    }];
    
    [self.contentView addSubview:self.availableNumLabel];
    [self.availableNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(availableLabel.mas_top);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(17);
    }];
    
    
    CGFloat btnW = (K_SCREENWIDTH - 30 - 30 - 20)/2.0;
    ///!!!:充值
    UIButton *rechargeBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"充值") titleColor:UIColorHex(0x678DA8) fontSize:14];
    rechargeBtn.backgroundColor = UIColorHex(0xFFFFFF);
    rechargeBtn.cornerRadius = 4;
    [self.contentView addSubview:rechargeBtn];
    [rechargeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.bottom.mas_equalTo(-35);
        make.width.mas_equalTo(btnW);
        make.height.mas_equalTo(40);
    }];
    
    
    
    ///!!!:质押
    [self.contentView addSubview:self.pledgeBtn];
    [self.pledgeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.bottom.mas_equalTo(-35);
        make.width.mas_equalTo(btnW);
        make.height.mas_equalTo(40);
    }];
    
    //账单 我的收益
    UILabel *billLab = [UILabel lz_labelWithText:[NSString stringWithFormat:@"USDT %@",LZLocalizedString(@"账单")] fontSize:k_textFont(13) color:UIColorHex(0xFFFFFF)];
    [self addSubview:billLab];
    [billLab mas_makeConstraints:^(MASConstraintMaker *make) {
           make.left.mas_equalTo(15);
           make.top.mas_equalTo(self.contentView.mas_bottom).mas_offset(21);
           make.width.mas_equalTo(150);
           make.height.mas_equalTo(21);
    }];
    
    ///我的收益
    UIButton *incomeBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"我的收益") titleColor:UIColorHex(0x4AB2E7) fontSize:12];
    [incomeBtn setImage:k_imageName(@"ico_next_xiaojiantou") forState:UIControlStateNormal];
    [self addSubview:incomeBtn];
    [incomeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
          make.right.mas_equalTo(-18);
          make.centerY.mas_equalTo(billLab.mas_centerY);
          make.width.mas_equalTo(100);
          make.height.mas_equalTo(17);
    }];
    incomeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [incomeBtn SG_imagePositionStyle:SGImagePositionStyleRight spacing:6];
    [[incomeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        
        ShellProfitViewController *profitVC = [[ShellProfitViewController alloc] init];
        profitVC.wallertModel = self.viewModel.wallertModel;
        [LZTool.currentViewController.navigationController pushViewController:profitVC animated:YES];
    }];
    
    //更换语言
    @weakify(self);
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:k_APPLanguageWillChangeNotificationName object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        
        [incomeBtn setTitle:LZLocalizedString(@"我的收益") forState:UIControlStateNormal];
        [incomeBtn SG_imagePositionStyle:SGImagePositionStyleRight spacing:6];
        billLab.text = [NSString stringWithFormat:@"USDT %@",LZLocalizedString(@"账单")];
        holdLabel.text = LZLocalizedString(@"持有数量");
        [coinBtn setTitle:LZLocalizedString(@"提币") forState:UIControlStateNormal];
        [self.liquidationBtn setTitle:LZLocalizedString(@"清算") forState:UIControlStateNormal];

        frozenLabel.text = LZLocalizedString(@"冻结数量");
        availableLabel.text = LZLocalizedString(@"可用数量");
        
        [rechargeBtn setTitle:LZLocalizedString(@"充值") forState:UIControlStateNormal];
        [self.pledgeBtn setTitle:LZLocalizedString(@"质押") forState:UIControlStateNormal];
        [self.pledgeBtn setTitle:LZLocalizedString(@"闪兑") forState:UIControlStateSelected];

        
        
        CGFloat coinBtnW = [coinBtn.currentTitle lz_textWidthWithFontSize:coinBtn.titleLabel.font withMaxHeight:17];
        [coinBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(coinBtnW);
        }];

        CGFloat liquidationBtnW = [self.liquidationBtn.currentTitle lz_textWidthWithFontSize:self.liquidationBtn.titleLabel.font withMaxHeight:17];
        [self.liquidationBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(liquidationBtnW);
        }];
    }];
    
    
    self.numberLab.text = @"0";
    self.frozenNumLabel.text = @"0";
    self.availableNumLabel.text = @"0";

    [self refreshUI];

    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        
        
        switch ([x integerValue]) {
            case 3:{
                NSString *balance = [NSString stringWithFormat:@"%@",self.viewModel.wallertModel.balance?:@"0"].scientific;
                NSString *freeze = [NSString stringWithFormat:@"%@",self.viewModel.wallertModel.freeze?:@"0"].scientific;
                NSString *useable = [NSString stringWithFormat:@"%@",self.viewModel.wallertModel.useable?:@"0"].scientific;
                self.numberLab.text = balance;
                self.frozenNumLabel.text = freeze;
                self.availableNumLabel.text = useable;
                
            }
                break;
            case 5:
            {
                [self refreshUI];
            }
            break;
                
            default:
                break;
        }
    }];
 
    
    self.pledgeBtn.tag = 10;
    self.liquidationBtn.tag = 11;
    coinBtn.tag = 12;
    rechargeBtn.tag = 13;

    [self.pledgeBtn addTarget:self action:@selector(sendActions:) forControlEvents:UIControlEventTouchUpInside];
    [self.liquidationBtn addTarget:self action:@selector(sendActions:) forControlEvents:UIControlEventTouchUpInside];
    [rechargeBtn addTarget:self action:@selector(sendActions:) forControlEvents:UIControlEventTouchUpInside];
    [coinBtn addTarget:self action:@selector(sendActions:) forControlEvents:UIControlEventTouchUpInside];
}



#pragma mark - actions
//点击事件
- (void)sendActions:(UIButton *)sender{
    
    
    NSLog(@"--------");
    switch (sender.tag) {
        case 10:{
                
            if([self.viewModel.currentCurrncy isEqualToString:@"USDT"]){
                //质押
                ShellPledgeViewController *pledgeVC = [[ShellPledgeViewController alloc] init];
                [LZTool.currentViewController.navigationController pushViewController:pledgeVC animated:YES];
            } else {
                //闪兑
                ShellExchangeViewController *exchangeVC = [[ShellExchangeViewController alloc] init];
                exchangeVC.wallertModel = self.viewModel.wallertModel;
                [LZTool.currentViewController.navigationController pushViewController:exchangeVC animated:YES];
            }
        }
            break;
        case 11:{
            //清算
            ShellLiquidationViewController *liquidationVC = [[ShellLiquidationViewController alloc] init];
            [LZTool.currentViewController.navigationController pushViewController:liquidationVC animated:YES];
        }
            break;
        case 12:{
            //提币
            ShellWithdrawViewController *coinVC = [[ShellWithdrawViewController alloc] init];
            coinVC.wallertModel = self.viewModel.wallertModel;
            [LZTool.currentViewController.navigationController pushViewController:coinVC animated:YES];
        }
            break;
        case 13:{
            //充币
            ShellRechargeViewController *rechargeVC = [[ShellRechargeViewController alloc] init];
            rechargeVC.wallertModel = self.viewModel.wallertModel;
            [LZTool.currentViewController.navigationController pushViewController:rechargeVC animated:YES];

        }
            break;
        default:
            break;
    }
    
  
}


///刷新UI
- (void)refreshUI {
    
    if([self.viewModel.currentCurrncy isEqualToString:@"USDT"]){
        self.currencyImageView.image = k_imageName(@"icon_usdt");
        [self.contentView setGradientBackgroundWithColors:@[UIColorHex(0x22C6FE),UIColorHex(0x65DFFE)] locations:@[@(0), @(1.0f)] startPoint:CGPointMake(0, 0.5) endPoint:CGPointMake(1, 0.5)];
        self.liquidationBtn.hidden = NO;
        self.pledgeBtn.selected = NO;
        
    } else if ([self.viewModel.currentCurrncy isEqualToString:@"BTC"]){
        self.currencyImageView.image = k_imageName(@"icon_btc");
        [self.contentView setGradientBackgroundWithColors:@[UIColorHex(0x4149FC),UIColorHex(0x919DFF)] locations:@[@(0), @(1.0f)] startPoint:CGPointMake(0, 0.5) endPoint:CGPointMake(1, 0.5)];
        self.liquidationBtn.hidden = YES;
        self.pledgeBtn.selected = YES;
    }
    else {
        self.currencyImageView.image = k_imageName(@"icon_eth");
        [self.contentView setGradientBackgroundWithColors:@[UIColorHex(0x097BC0),UIColorHex(0x4DBAEE)] locations:@[@(0), @(1.0f)] startPoint:CGPointMake(0, 0.5) endPoint:CGPointMake(1, 0.5)];
        self.liquidationBtn.hidden = YES;
        self.pledgeBtn.selected = YES;

    }
}
#pragma mark - custool

#pragma mark - get && set
- (UIView *)contentView{
    if(!_contentView){
        _contentView = [[UIView alloc] init];
        _contentView.cornerRadius = 6;
        [_contentView setGradientBackgroundWithColors:@[UIColorHex(0x65DFFE),UIColorHex(0x22C6FE)] locations:@[@(0), @(1.0f)] startPoint:CGPointMake(0, 0.5) endPoint:CGPointMake(1, 0.5)];
    }
    return _contentView;
}


- (UIImageView *)currencyImageView{
    if(!_currencyImageView){
        _currencyImageView = [[UIImageView alloc] initWithImage:k_imageName(@"icon_usdt")];
    }
    return _currencyImageView;
}


- (UILabel *)numberLab{
    if (!_numberLab) {
        _numberLab = [UILabel lz_createLabWithTextColor:UIColorHex(0xFFFFFF) font:k_textBoldFont(30)];
    }
    return _numberLab;
}

///清算
- (UIButton *)liquidationBtn{
    if(!_liquidationBtn){
        _liquidationBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"清算") titleColor:UIColorHex(0x576E98) fontSize:12];
    }
    return _liquidationBtn;
}



- (UILabel *)frozenNumLabel{
    if (!_frozenNumLabel) {
        _frozenNumLabel = [UILabel lz_createLabWithTextColor:UIColorHex(0xFFFFFF) font:k_textBoldFont(14)];
        [_frozenNumLabel lz_rightAlignment];
    }
    return _frozenNumLabel;
}
- (UILabel *)availableNumLabel{
    if (!_availableNumLabel) {
        _availableNumLabel = [UILabel lz_createLabWithTextColor:UIColorHex(0xFFFFFF) font:k_textBoldFont(14)];
        [_availableNumLabel lz_rightAlignment];

    }
    return _availableNumLabel;
}


- (UIButton *)pledgeBtn{
    if(!_pledgeBtn){
        _pledgeBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"质押") titleColor:UIColorHex(0xFFFFFF) fontSize:14];
        [_pledgeBtn setTitle:LZLocalizedString(@"闪兑") forState:UIControlStateSelected];
        _pledgeBtn.bColor = UIColorHex(0xFFFFFF);
        _pledgeBtn.bWidth = 1;
        _pledgeBtn.cornerRadius = 4;
      }
      return _pledgeBtn;
}


@end
